package com.focus.wecenter.web.controller;

import com.focus.wecenter.pojo.Topic;
import com.focus.wecenter.service.QuestionService;
import com.focus.wecenter.service.TopicService;
import com.focus.wecenter.util.Message;
import com.focus.wecenter.util.PageBean;
import com.focus.wecenter.web.controller.common.BaseController;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 话题模块
 *
 * @author Lv Jing
 * @date 2018年01月08日 0:05
 */
@Controller
@RequestMapping("/topic")
public class TopicController extends BaseController {

    @Autowired
    private TopicService topicService;
    @Autowired
    private QuestionService questionService;
    @Value("${topic.pagesize}")
    private int pageSize = 10;
    @Value("${focus.topic.pagesize}")
    private int focusTopicPageSize;

    /**
     * 话题广场首页
     */
    @RequestMapping(value = {"", "/all", "/week", "/monty"})
    public String topicIndex(HttpServletRequest request) {
        return "forward:/topic/all/1";
    }

    /**
     * 话题广场首页的全部tab分页
     */
    @RequestMapping("/all/{page}")
    public String showTopicSquare(@PathVariable int page, Model model) {
        model.addAttribute("result", topicService.findByAll(page, pageSize));
        model.addAttribute("recent", topicService.findByRecently(page, 10));
        return "topic/list";
    }

    /**
     * 话题广场7天tab分页
     */
    @RequestMapping("/week/{page}")
    public String topicSquareWeek(@PathVariable int page, Model model) {
       model.addAttribute("result", topicService.findByWeek(page, pageSize));
       model.addAttribute("recent", topicService.findByRecently(page, 10));
       return "topic/week";
    }

    /**
     * 话题广场30天tab分页
     */
    @RequestMapping("/month/{page}")
    public String topicSquareMonty(@PathVariable int page, Model model) {
        model.addAttribute("result", topicService.findByMonth(page, pageSize));
        model.addAttribute("recent", topicService.findByRecently(page, 10));
        return "topic/month";
    }

    /**
     * 显示话题详情页面
     */
    @RequestMapping("/{topicID}")
    public String topicDetail(@PathVariable int topicID, Model model, HttpSession session) {
        Topic topic = topicService.findTopicByID(topicID);
        model.addAttribute("topic", topic);
        boolean focus = topicService.isFocus(topicID, super.getSessionUserID(session));
        model.addAttribute("focus", focus);
        return "topic/detail";
    }

    /**
     * 加载关注话题列表数据
     */
    @RequestMapping("/ajax/focus_topics_list/page-{page}")
    public String getFocusTopics(@PathVariable int page, Model model, HttpSession session) {
        DetachedCriteria criteria = DetachedCriteria.forClass(Topic.class);
        criteria.createAlias("focusUsers", "focus")
                .add(Restrictions.eq("focus.id", this.getSessionUserID(session)));
        PageBean<Topic> result = topicService.findByPage(criteria, page, focusTopicPageSize);
        model.addAttribute("result", result.getList());
        return "topic/template_list";
    }

    /**
     * 关注话题
     */
    @RequestMapping(value = "/ajax/focus", method = RequestMethod.POST)
    @ResponseBody
    public Message focusTopic(@RequestParam("topic_id") int topicID, HttpSession session) {
        try {
            return topicService.focus(topicID, this.getSessionUserID(session));
        } catch (Exception e) {
            Message message = new Message();
            message.setSuccess(false);
            message.setData("服务器未知异常");
            e.printStackTrace();
            return message;
        }
    }

    /*@RequestMapping("/ajax/list/sort_type-new__topic_id-{topicID}__page-{page}")
    public String getQuestions(@PathVariable int topicID, @PathVariable int page, Model model) {
        // FIXME：分页有问题
        DetachedCriteria criteria = DetachedCriteria.forClass(Question.class);
        criteria.add(Restrictions.eq("involvedTopics.id", topicID));
        criteria.addOrder(Order.desc("createTime"));
        Topic topic = topicService.findTopicByID(topicID);
        model.addAttribute("result", topic.getInvolveQuestions());
        return "question/template_list";
    }*/
}
